//
//  CWCImageViewController.m
//  ChuanNews
//
//  Created by dllo on 16/4/21.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CWCImageViewController.h"
#import "BaseHeader.h"
#import "CWCPhotoDetailViewController.h"

@interface CWCImageViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, retain)NSTimer *outTimer;
@property(nonatomic, assign)NSInteger page;
@end

@implementation CWCImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.TableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [self.view addSubview:self.TableView];
    self.TableView.dataSource = self;
    self.TableView.delegate = self;
    [self.TableView release];
    self.TableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        if (_outTimer) {
            [_outTimer invalidate];
            _outTimer = nil;
        }
        self.outTimer = [NSTimer scheduledTimerWithTimeInterval:10.0f target:self selector:@selector(showNetWorkBad) userInfo:nil repeats:NO];
        [self creatData];
        
    }];
    [self.TableView.mj_header beginRefreshing];
    self.array = [NSMutableArray array];
    self.page = 1;
    [self setupFooder];
    [self.TableView registerClass:[CWCimgCellBig class] forCellReuseIdentifier:@"cell"];
    [self.TableView registerClass:[CWCRightImgCell class] forCellReuseIdentifier:@"cellA"];
    [self.TableView registerClass:[CWCLeftImgCell class] forCellReuseIdentifier:@"cellB"];
    
}
-(void)creatData{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"http://c.m.163.com/photo/api/list/0096/54GI0096.json" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *dataArray = responseObject;
        [self.array removeAllObjects];
        for (int i = 0; i < dataArray.count; i++ ) {
            NSDictionary *dic = dataArray[i];
            
            NSArray *picArray = dic[@"pics"];
            if (picArray.count > 0) {
            NSString *imgurl = picArray[0];
            NSString *imgurlA = picArray[1];
            NSString *imgurlB = picArray[2];

            CWCImgModel *imgModel = [[CWCImgModel alloc]init];
            [imgModel setValuesForKeysWithDictionary:dic];
            [imgModel setValue:imgurl forKey:@"img1"];
            [imgModel setValue:imgurlA forKey:@"img2"];
            [imgModel setValue:imgurlB forKey:@"img3"];
            [self.array addObject:imgModel];
                [imgModel release];
        }
        }
        [self.TableView.mj_header endRefreshing];
        if (_outTimer) {
            [_outTimer invalidate];
            _outTimer = nil;
        }
        [self.TableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
        
    }];
    
}

-(void)setupFooder{
    self.TableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self refreshFooterView];
    }];
}
-(void)refreshFooterView{
    CWCImgModel *model = self.array.lastObject;
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    [manager GET:[NSString stringWithFormat:@"http://c.m.163.com/photo/api/morelist/0096/54GI0096/%@.json",model.setid] parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSMutableArray *tempModelArray = [NSMutableArray array];
        NSArray *dataArray = responseObject;
        for (int i = 0; i < dataArray.count; i++) {
            NSDictionary *dic = dataArray[i];
            NSArray *picArray = dic[@"pics"];
            if (picArray.count != 0) {
            NSString *imgUrl= picArray[0];
            NSString *imgUrlA= picArray[1];
            NSString *imgUrlB= picArray[2]; 
            CWCImgModel *imgModel = [[CWCImgModel alloc]init];
            [imgModel setValuesForKeysWithDictionary:dic];
            [imgModel setValue:imgUrl forKey:@"img1"];
            [imgModel setValue:imgUrlA forKey:@"img2"];
            [imgModel setValue:imgUrlB forKey:@"img3"];
            [tempModelArray addObject:imgModel];
            }
        }
        [self.array addObjectsFromArray:tempModelArray];
        [self.TableView reloadData];
        _page++;
        [self.TableView.mj_footer setState:MJRefreshStateIdle];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@",error);
    }];
   
}
-(void)showNetWorkBad{
    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您当前的网络不给力，请重新刷新！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
    if (_outTimer) {
        [_outTimer invalidate];
        _outTimer = nil;
    }
    [self.TableView.mj_header endRefreshing];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CWCImgModel *model = self.array[indexPath.row];
    int index = indexPath.row % 3;
    if (index == 0) {
        CWCimgCellBig * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.img1]];
        cell.titleLabel.text = model.setname;
        cell.titleLabel.font = [UIFont systemFontOfSize:16];
        return cell;
    }else if (index == 1){
        CWCRightImgCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellA"];
        [cell.imageViewA sd_setImageWithURL:[NSURL URLWithString:model.img1]];
        [cell.imageViewB sd_setImageWithURL:[NSURL URLWithString:model.img2]];
        [cell.imageViewC sd_setImageWithURL:[NSURL URLWithString:model.img3]];
        cell.titleLabel.text = model.setname;
        cell.titleLabel.font = [UIFont systemFontOfSize:16];
        return cell;
    }else{
        CWCLeftImgCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellB"];
        [cell.imgViewA sd_setImageWithURL:[NSURL URLWithString:model.img1]];
        [cell.imgViewB sd_setImageWithURL:[NSURL URLWithString:model.img2]];
        [cell.imgViewC sd_setImageWithURL:[NSURL URLWithString:model.img3]];
        cell.titleLabel.text = model.setname;
        cell.titleLabel.font =[ UIFont systemFontOfSize:16];
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    int index = indexPath.row % 3;
    if (index == 0) {
        return 300;
    }
    
    return 200;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CWCPhotoDetailViewController *vc = [[CWCPhotoDetailViewController alloc]init];
    CWCImgModel *model = self.array[indexPath.row];
    NSString *str = [NSString stringWithFormat:@"0096/%@",model.setid];
    vc.setid = str;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];

}

-(void)dealloc{
    [_outTimer release];
    [_TableView release];
    [super dealloc];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
